package com.business.system.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.business.base.action.BaseAction;
import com.business.base.tools.JsonUtil;
import com.business.system.pojo.TabCarOwnerInfo;
import com.business.system.service.ICarOwnerInfoService;
import com.utility.Page;

@Controller
@ParentPackage("struts-default")
@Namespace("/")
public class CarOwnerInfoAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private TabCarOwnerInfo carOwnerInfo;
	@Autowired
	private ICarOwnerInfoService carOwnerInfoService;
	private String id;
	private String car_tag_number;

	@Action(value = "CarOwnerInfoAction_list")
	public String list() throws Exception {
		this.pageNo = (int) Math.floor((start + limit) / limit);
		if (paramsCode != null && !paramsCode.equals("")) {
			carOwnerInfo = (TabCarOwnerInfo) JsonUtil.getObject4JsonString(
					paramsCode, TabCarOwnerInfo.class);
		} else {
			carOwnerInfo = new TabCarOwnerInfo();
		}

		Page<TabCarOwnerInfo> page = this.carOwnerInfoService.findListPage(
				this.carOwnerInfo, this.pageNo, this.limit);
		List<TabCarOwnerInfo> list = page.getResults();
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + page.getTotalRecord() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "CarOwnerInfoAction_getByTag")
	public String getByTag() throws Exception {
		carOwnerInfo = new TabCarOwnerInfo();
		carOwnerInfo.setCarTagNumber(this.car_tag_number);

		List<TabCarOwnerInfo> list = this.carOwnerInfoService
				.findList(carOwnerInfo);
		if (list.size() == 0) {
			String result = "{success:false}";
			this.jsonOut(result);
			return NONE;
		} else {
			JSONArray array = JSONArray.fromObject(list.get(0));
			String result = "{success:true,root:" + array.toString() + "}";
			this.jsonOut(result);
			return NONE;
		}
	}

	@Action(value = "CarOwnerInfoAction_add")
	public String add() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			carOwnerInfo = (TabCarOwnerInfo) JsonUtil.getObject4JsonString(
					paramsCode, TabCarOwnerInfo.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.carOwnerInfoService.insert(carOwnerInfo);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "CarOwnerInfoAction_edit")
	public String edit() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			carOwnerInfo = (TabCarOwnerInfo) JsonUtil.getObject4JsonString(
					paramsCode, TabCarOwnerInfo.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.carOwnerInfoService.update(this.carOwnerInfo);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "CarOwnerInfoAction_delete")
	public String delete() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		try {
			this.carOwnerInfoService.delete(id);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public TabCarOwnerInfo getCarOwnerInfo() {
		return carOwnerInfo;
	}

	public void setCarOwnerInfo(TabCarOwnerInfo carOwnerInfo) {
		this.carOwnerInfo = carOwnerInfo;
	}

	public String getCar_tag_number() {
		return car_tag_number;
	}

	public void setCar_tag_number(String car_tag_number) {
		this.car_tag_number = car_tag_number;
	}

}
